package com.uff.hmstpa.model.persistence.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.uff.hmstpa.model.persistence.AbstractModelDAO;
import com.uff.hmstpa.model.persistence.CoverageDAO;

public class CoverageDAOImpl extends AbstractModelDAO implements CoverageDAO {

	private static CoverageDAOImpl SOLE_INSTANCE;

	private CoverageDAOImpl() {
		super();
	}

	public static CoverageDAO getInstance() {
		if (SOLE_INSTANCE == null) {
			SOLE_INSTANCE = new CoverageDAOImpl();
		}
		return SOLE_INSTANCE;
	}

	public Map<Integer, Set<Integer>> load() {
		Map<Integer, Set<Integer>> coverage = new HashMap<Integer, Set<Integer>> ();

		List<Map<String, Object>> rs = jdbcTemplate.queryForList("SELECT * FROM coverage");

		for(int i = 0; i < rs.size(); i++) {
			Map<String, Object> row = rs.get(i);
			int hpCode = Integer.parseInt(row.get("healthplan").toString());
			int spcCode = Integer.parseInt(row.get("specialty").toString());

			if(coverage.get(hpCode) == null) {
				coverage.put(hpCode, new HashSet<Integer>());
			}			
			
			coverage.get(hpCode).add(spcCode);

		}

		return coverage;
	}

}
